package com.restful.dao;

import java.util.ArrayList;
import java.util.List;

import javax.management.RuntimeErrorException;
import javax.ws.rs.DELETE;
import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Request;
import javax.ws.rs.core.UriInfo;

import com.restful.resources.UserInfoResources;
import com.restful.test.UserInfo;

/*
 * A UserInfoDao which maps RESTful path to UserInfo data manipulation methods
 * Sample URL: 
 * GET/POST/DELETE http://localhost:8080/test/JaxbService/userInfo/{Optional: key}
 * */

@Path("/userInfo")
public class UserInfoDao {
	@Context
	UriInfo uriInfo;
	@Context
	Request request;
	String name;
	
	public UserInfoDao()
	{
	}
	public UserInfoDao(UriInfo uriInfo, Request request, String name){
		this.uriInfo = uriInfo;
		this.request = request;
		this.name = name;
	}
	
	@GET
	@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
	public UserInfo userInfoXml(){
		UserInfo userInfo = UserInfoResources.instance.getModel().get(name);
		if(userInfo == null)
			throw new RuntimeErrorException(null, "required resource doesn't exist.");
		return userInfo;
	}
	
	@GET
	@Produces({MediaType.TEXT_XML,MediaType.APPLICATION_OCTET_STREAM})
	public UserInfo userInfoHtml(){
		UserInfo userInfo = UserInfoResources.instance.getModel().get(name);
		if(userInfo == null)
			throw new RuntimeErrorException(null, "required resource doesn't exist.");
		return userInfo;
	}
	/*
	 * Get the entry of a user with key 'name'
	 * */
	@GET
	@Path("{name}")
	public UserInfoDao getUser(@PathParam("name") String name){
		return new UserInfoDao(uriInfo,request,name);
	}
	
	/*
	 * Get the user list
	 * Sample URL: {domain}/userInfo/getUserList
	 * */
	@GET
	@Path("getUserList")
	public List<UserInfo> getUserList(){
		List<UserInfo> userList = new ArrayList<UserInfo>();
		userList.addAll(UserInfoResources.instance.getModel().values());
		return userList;
	}
	
	/*
	 * create a new user with name/address
	 * Sample URL: 
	 * POST {domain}/userInfo/ 
	 * post body: "name=abc&address=def"
	 * */
	@POST
	@Path("addUser")
	public UserInfo addUser(@FormParam("name") String name,
						@FormParam("address") String address){
		UserInfo item = new UserInfo(name,address);
		UserInfoResources.instance.getModel().put(name, item);
		return item;
	}
	
	/*
	 * Delete a user with key 'name'
	 * Sample URL:
	 * DELETE {domain}/userInfo/abc
	 * */
	@DELETE
	@Path("deleteUser/{userName}")
	public UserInfo deleteUser(@PathParam("userName") String name){
		try{
			UserInfo item = UserInfoResources.instance.getModel().remove(name);
			return item;
		}
		catch(Exception e){
			System.out.println("request user cannot be deleted.");
			e.printStackTrace();
		}
		return null;
	}
}
